<?php
header('Content-Type:text/html; charset=utf-8');
$db=new mysqli('localhost','root','','db_bbs','3306');
if($db->connect_error){
    die("连接失败，".$db->connect_error);
}
$db->query("set names utf8");



$search=(isset($_GET['search']))?$_GET['search']:"";
$sql="select * from t_message where message like '%{$search}%' or username like '%{$search}%'";
$result=$db->query($sql);//$result是一个mysqli的结果集

$hu=5;
$pg=  isset($_GET['pg'])?$_GET['pg']:1;
$gg=$result->num_rows;
$wei=  ceil($gg/$hu);
$pg=($pg<=0)?1:$pg;
$pg=($pg>$wei)?$wei:$pg;
$qei=($pg-1)*$hu;
$sql="select * from t_message where message like '%{$search}%' or username like '%{$search}%' order by id desc  limit {$qei},{$hu}";

//$sql="select * from t_message order by id desc";
$result=$db->query($sql);
$sum=$result->num_rows;
$rows=$result->fetch_all(MYSQLI_ASSOC);


